package com.ea.eadp.nexus.test.pageDefinition.originX;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;

import com.ea.eadp.nexus.test.common.PageOperation;
import com.ea.eadp.nexus.test.pageDefinition.BasePage;

public class SignupUnderagePage extends BasePage {

    private static final String PARENT_EMAIL_ID = "parentEmail";
    private static final String PASSWORD_ID = "password";
    private static final String PUBLIC_ID_ID = "originId";
    private static final String RECPATCHA_ID = "recaptcha_response_field";
    private static final String CREATE_ACCOUNT_BUTTON_ID = "basicInfoNextBtn";

    public SignupUnderagePage(final WebDriver driver) {
        super(driver, UnderageEmailVerificationPage.class);
    }

    public void inputParentEmail(final String parentEmail) {
        PageOperation.SendKeyToElementAfterWait(driver, PARENT_EMAIL_ID, parentEmail);
    }

    public void inputPassword(final String password) {
        PageOperation.SendKeyToElementAfterWait(driver, PASSWORD_ID, password);
    }

    public void inputPublicId(final String id) {
        PageOperation.SendKeyToElementAfterWait(driver, PUBLIC_ID_ID, id);
    }

    public void inputRecaptcha(final String recaptcha) {
        PageOperation.SendKeyToElementAfterWait(driver, RECPATCHA_ID, recaptcha);
    }

    public void validateCreateAccountButtonDisabled() throws InterruptedException {
        for (int i = 0; i < 5; i++) {
            if (driver.findElement(By.id(CREATE_ACCOUNT_BUTTON_ID)).getAttribute("class").contains("disabled")) {
                return;
            } else {
                Thread.sleep(100);
            }
        }
        Assert.fail("Create account button is not disabled.");
    }

    public void validateInvalidPassword() {
        validateErrorMassage("form-error-password-complexity", "Password does not meet requirements.");
    }

    public void validateTooShortPublicId() {
        validateErrorMassage("form-error-origin-id-too-short", "ID must be between 4 and 16 characters.");
    }

    public void validateTooLongPublicId() {
        validateErrorMassage("form-error-origin-id-too-long", "ID must be between 4 and 16 characters.");
    }

    public void validateInvalidPublicId() {
        validateErrorMassage("form-error-origin-id-invalid", "ID contains a prohibited word or character");
    }

    public void clickCreateAccountButton() {
        driver.findElement(By.id(CREATE_ACCOUNT_BUTTON_ID)).click();
    }
}
